<?php

namespace App\Http\Controllers\Home;


use App\Models\Follow;
use App\Models\Remind;
use App\Models\Reply;
use App\Models\Section;
use App\Models\Tag;
use App\Models\TopicBbs;
use App\Models\TopicTag;
use App\Models\User;
use App\Models\Vote;
use Illuminate\Http\Request;
use EndaEditor;
use App\Http\Controllers\Controller;

class TopicsController extends Controller
{
    //文章内容界面
    public function topic_content(Request $request,$id)
    {
        $topic = TopicBbs::with('user', 'profile', 'replies','vote')->find($id);
        $old_view=$topic->view_num;
        TopicBbs::where('id',$id)->update(['view_num'=>$old_view+1]);
        $voted=0;
        $followed=0;
        if ($request->session()->has('userModel')){
            foreach ($topic->vote as $b){
                if ($b->user_id==$request->session()->get('userModel')->id){
                    $voted=1;
                }
            }
            $my=User::with('follow')->find($request->session()->get('userModel')->id);
            foreach ($my->follow as $f){
                if ($f->another_id==$topic->user->id){
                    $followed=1;
                }
            }
        }
        return view('home/TopicsViews/Topic', ['topic' => $topic,'voted'=>$voted,'followed'=>$followed]);
    }
    //文章发表界面
    public function topic_postView()
    {
        $section = Section::all();
        $tags = Tag::all();
        return view('home/TopicsViews/Topic_post', ['section' => $section, 'tags' => $tags]);
    }
    //文章发表方法
    public function topic_post(Request $request)
    {
        $section = $request->input('section');
        $title = $request->input('title');
        $tag = $request->input('tags');
        $content = $request->input('content');
        $topic = new TopicBbs;
        $topic->section = $section;
        $topic->title = $title;
        $topic->content = $content;
        $topic->user_id = $request->session()->get('userModel')['id'];
        $topic->save();

        if (!empty($tag)) {
            foreach ($tag as $a) {
                $topic_tag = new TopicTag;
                $topic_tag->topic_id = $topic->id;
                $topic_tag->tag_id = $a;
                $topic_tag->save();
            }
        }

        $remind=new Remind;
        $remind->user_id=$topic->user_id;
        $remind->topic_id=$topic->id;
        $remind->desc='created_topic';
        $remind->save();

        return redirect('myUserCenter')->with('green_msg', '发表成功');
    }
    //文章图片上传
    public function topic_image(Request $request)
    {
        if ($request->hasFile('upload')) {     //upload为ckeditor默认的file提交ID
            $file = $request->file('upload');   //从请求数据内容中取出图片的内容
            $allowed_extensions = ["png", "jpg", "gif"]; //允许的图片后缀
            if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                return '图片后缀只支持png,jpg,gif,请检查！';
            }
            $destinationPath = 'home/upload/';  //图片存放路径
            $extension = $file->getClientOriginalExtension();  //获得文件后缀
            $fileName = str_random(10) . '.' . $extension;  //创建图片名字
            $result = $file->move($destinationPath, $fileName); //存储图片到路径
            echo url('') . '/' . $result; //输出图片网站中浏览路径
        }
    }
    //文章评论
    public function reply_add(Request $request){
        $content = $request->input('content');
        $user_id = $request->input('user_id');
        $topic_id = $request->input('topic_id');
        $replyer = $request->input('replyer');

        $rep = '';
        $sender_id = '';
        $replyers = Reply::where('topic_id', $topic_id)->get();
        foreach ($replyers as $r) {
            $rr = $r->user->username;
            if ($rr == $replyer) {
                $rep = $replyer;
            }
        }
        if ($rep != '') {
            $sender = User::where('username', $rep)->get();
            $sender_id = $sender[0]->id;
        }

        if ($content != '') {
            $reply=new Reply;
            $reply->user_id=$user_id;
            $reply->topic_id=$topic_id;
            $reply->content=$content;
            $reply->answer_id=$sender_id;
            $reply->save();
            $remind=new Remind;
            $remind->user_id=$user_id;
            $remind->another_id=$reply->answer_id;
            $remind->answer_content=$content;
            $remind->topic_id=$topic_id;
            $remind->desc='answer';
            $remind->reply_id=$reply->id;
            $remind->save();
        }
        //每一篇文章的评论一次增加一次记录
        $old_number=TopicBbs::find($topic_id)->reply_number;
        TopicBbs::where('id',$topic_id)->update(['reply_number'=>$old_number+1]);

        $replies = Reply::where('topic_id', $topic_id)->get();
        $created_at = $reply->created_at->diffForHumans();
        $reply_id = $reply->id;

        return response()->json([
            'content' => $content, 'replies' => $replies, 'created_at' => $created_at, 'reply_id' => $reply_id, 'replyer' => $rep
        ]);
    }
    //删除文章
    public function delete_topic(Request $request){
        $topic_id=$request->input('topic_id');
        TopicBbs::where('id',$topic_id)->delete();
        Remind::where('topic_id',$topic_id)->delete();
        Reply::where('topic_id',$topic_id)->delete();
        TopicTag::where('topic_id',$topic_id)->delete();
        Vote::where('topic_id',$topic_id)->delete();
        return response()->json(array(
            'msg'=>'ok'
        ));
    }
    //修改文章界面
    public function update_topic_view($id){
        $topic=TopicBbs::with('sections','tags')->find($id);
        $section = Section::all();
        $tags = Tag::all();
        return view('home/TopicsViews/Update_topic', ['section' => $section, 'tags' => $tags,'topic' =>$topic]);
    }
    //修改文章方法
    public function update_topic(Request $request,$id)
    {
        $section = $request->input('section');
        $title = $request->input('title');
        $tag = $request->input('tags');
        $content = $request->input('content');
        $topic = TopicBbs::find($id);
        $topic->section = $section;
        $topic->title = $title;
        $topic->content = $content;
        $topic->save();
        $remind=new Remind;
        $remind->user_id=$request->session()->get('userModel')->id;
        $remind->topic_id=$id;
        $remind->desc='update_topic';
        $remind->save();
        if (!empty($tag)) {
            TopicTag::where('topic_id',$id)->delete();
            foreach ($tag as $a) {
                $topic_tag = new TopicTag;
                $topic_tag->topic_id = $topic->id;
                $topic_tag->tag_id = $a;
                $topic_tag->save();
            }
        }
        return redirect('myUserCenter')->with('green_msg', '修改成功');
    }
    //回复(@)
    public function answer_add(Request $request){
        if($request->session()->has('userModel')){
            $answer_id=$request->input('answer_id');
            $answer_name=User::find($answer_id);
            return response()->json(array(
                'msg'=>$answer_name->username
            ));
        }
        return response()->json(array(
            'msg'=>'no login'
        ));
    }
    //删除评论
    public function delete_reply(Request $request){
        $reply_id=$request->input('reply_id');
        $id=Reply::find($reply_id)->topic_id;
        Reply::where('id',$reply_id)->delete();
        $old_number=TopicBbs::find($id)->reply_number;
        TopicBbs::where('id',$id)->update(['reply_number'=>$old_number-1]);
        Remind::where('reply_id',$reply_id)->delete();
        return response()->json(array(
            'msg'=>'ok'
        ));
    }
    //点赞
    public function upVote(Request $request){
        if(empty($request->session()->get('userModel')->id)){
            return response()->json(array(
                'msg'=>'no login'
            ));
        }
        $topic_id=$request->input('topic_id');
        $user_id=$request->session()->get('userModel')->id;
        $user_avatar=asset(User::find($user_id)->profile->avatar);
        $vote=new Vote;
        $vote->topic_id=$topic_id;
        $vote->user_id=$user_id;
        $vote->save();
        $old_number=TopicBbs::find($topic_id)->vote_number;
        TopicBbs::where('id',$topic_id)->update(['vote_number'=>$old_number+1]);
        $remind=new Remind;
        $remind->user_id=$user_id;
        $remind->topic_id=$topic_id;
        $remind->desc='vote';
        $remind->save();
        return response()->json(array(
            'msg'=>'ok',
            'avatar'=>$user_avatar,
            'id'=>$user_id
        ));
    }
    //取消点赞
    public function voteDown(Request $request){
        $topic_id=$request->input('topic_id');
        $user_id=$request->session()->get('userModel')->id;
        $user_avatar=User::find($user_id)->profile->avatar;
        Vote::where('topic_id',$topic_id)->where('user_id',$user_id)->delete();
        $old_number=TopicBbs::find($topic_id)->vote_number;
        TopicBbs::where('id',$topic_id)->update(['vote_number'=>$old_number-1]);
        Remind::where('topic_id',$topic_id)->where('user_id',$user_id)->where('desc','vote')->delete();
        return response()->json(array(
            'avatar'=>$user_avatar,
            'id'=>$user_id
        ));
    }

}

